package com.chushouya.product.dao.repository;

import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.product.dao.entity.ProductImageRecordEntity;
import com.chushouya.product.dao.mapper.ProductImageRecordEntityMapper;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

/**
 * 产品图片生成记录Repository
 */
@Repository
public class ProductImageRecordRepository extends BaseRepository<ProductImageRecordEntityMapper, ProductImageRecordEntity, Long> {
    
    /**
     * 根据条件查询列表
     */
    public List<ProductImageRecordEntity> selectList(ProductImageRecordQuery query) {
        Example example = buildExample(query);
        return this.selectByExample(example);
    }

    /**
     * 根据条件分页查询
     */
    public PageResult<ProductImageRecordEntity> selectPageResult(ProductImageRecordQuery query) {
        Example example = this.buildExample(query);
        return this.selectPage(query.getPageRequest(), example);
    }

    /**
     * 根据条件查询单条记录
     */
    public ProductImageRecordEntity selectOne(ProductImageRecordQuery query) {
        Example example = buildExample(query);
        return this.selectFirstOneByExample(example);
    }

    /**
     * 构建查询条件
     */
    private Example buildExample(ProductImageRecordQuery query) {
        Example example = new Example(ProductImageRecordEntity.class);
        Example.Criteria criteria = example.createCriteria();
        
        if (Objects.nonNull(query.getRecordId())) {
            criteria.andEqualTo("recordId", query.getRecordId());
        }
        if (Objects.nonNull(query.getCategoryId())) {
            criteria.andEqualTo("categoryId", query.getCategoryId());
        }
        if (Objects.nonNull(query.getBrandId())) {
            criteria.andEqualTo("brandId", query.getBrandId());
        }
        if (Objects.nonNull(query.getProductId())) {
            criteria.andEqualTo("productId", query.getProductId());
        }
        if (Objects.nonNull(query.getStatus())) {
            criteria.andEqualTo("status", query.getStatus());
        }
        if (Objects.nonNull(query.getDate())) {
            criteria.andEqualTo("date", query.getDate());
        }
        
        // 按创建时间倒序
        example.orderBy("categoryId").asc().orderBy("brandId").asc();
        
        return example;
    }

    /**
     * 查询条件类
     */
    @lombok.Data
    public static class ProductImageRecordQuery extends com.general.framework.core.schema.PageRequest {
        private Long recordId;
        private Long categoryId;
        private Long brandId;
        private Long productId;
        private Integer status;
        private String date;
    }
}
